<?php

namespace App\Http\Search\Org;

use App\Http\Model\ConstCode;
use App\Http\Search\BaseSearch;
use App\Http\Model\Org\StaffModel;
use App\Http\Model\Org\DepartmentStaffModel;

class StaffSearch extends BaseSearch
{

    /**
     * @Desc 员工列表
     * @return array
     * @author yashuai
     */
    public function index()
    {
        $where = [
            ['Status', '=', ConstCode::BASIC_STATUS_NORMAL]
        ];
        if (_array_field($this->params, 'StaffName')) {
            $where[] = ['StaffName', 'like', "%{$this->params['StaffName']}%"];
        }
        if (_array_field($this->params, 'Mobile')) {
            $where[] = ['Mobile', '=', $this->params['Mobile']];
        }
        if (_array_field($this->params, 'DepartmentID')) {
            $where[] = ['DepartmentID', '=', $this->params['DepartmentID']];
        }
        if (_array_field($this->params, 'Gender', false, '0') !== false) {
            $where[] = ['Gender', '=', $this->params['Gender']];
        }
        $select      = [
            'StaffID', 'StaffName', 'StaffOrder', 'Mobile', 'Position', 'Gender',
            'Email', 'UserID', 'EnglishName', 'Status', 'CreateTime', 'DepartmentID'
        ];
        $this->query = StaffModel::query()->with('department')->where($where)
            ->select($select)
            ->orderBy('Position', 'DESC')
            ->orderBy('StaffOrder', 'DESC');
        $result = $this->pageList();
        foreach ($result['list'] as &$val){
            $val['DepartmentName'] = $val['department']['Name'] ?? '';
            unset($val['department']);
        }
        return $result;
    }

}
